/**
 * @FileName: RolePermission.java
 * @Author
 * @Description:
 * @Date 2015年12月24日 下午8:08:38
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

@TableBind(tableName = "sys_role_permission")
public class SysRolePermission extends Model<SysRolePermission>{
    private static final long             serialVersionUID = -5113277320191605364L;
    public static final SysRolePermission dao              = new SysRolePermission();

    public List<Integer> getPermissionIds(int roleId){
        List<SysRolePermission> list = dao.find("select permission_id from sys_role_permission where role_id=?",
                                                roleId);
        List<Integer> permIdList = new ArrayList<Integer>();
        for(SysRolePermission permission : list){
            // System.out.println("permissionId=" + permission);
            permIdList.add(permission.getInt("permission_id"));
        }
        return permIdList;
    }

    public void deleteRolePermission(int roleId){
        Db.update("DELETE FROM sys_role_permission WHERE role_id=?", roleId);
    }

    public boolean deleteByRoleIdAndPermissionId(String roleId, String permissionId){
        List<Object> paras = new ArrayList<Object>();

        String sql = " delete from sys_role_permission where is_deleted = 0 and role_id = ? and permission_id = ? ";
        paras.add(roleId);
        paras.add(permissionId);
        int rc = Db.update(sql, paras.toArray());

        return rc > 0;

    }
}
